package org.cerberus.platform.ograph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectGraphTransaction {

	private Map<String, ComponentClass> addClasses;
	private List<String> removeClasses;
	private Map<Integer, Object> addObjects;
	private List<Integer> removeObjects;
	private Map<Integer, Integer> addConnections;
	private Map<Integer, Integer> removeConnections;
	
	void addComponentClass(ComponentClass clazz) {
		if (addClasses == null) {
			addClasses = new HashMap<String, ComponentClass>();
		}
		if (removeClasses != null 
				&& removeClasses.contains(clazz.getClassID())) {
			removeClasses.remove(clazz.getClassID());
			if (removeClasses.size() == 0) {
				removeClasses = null;
			}
		}
		addClasses.put(clazz.getClassID(), clazz);
	}
	
	void removeComponentClass(String classID) {
		if (removeClasses == null) {
			removeClasses = new ArrayList<String>();
		}
		if (addClasses != null && addClasses.containsKey(classID)) {
			addClasses.remove(classID);
			if (addClasses.size() == 0) {
				addClasses = null;
			}
		}
		removeClasses.add(classID);
	}
	
	void addObject(int hash, Object o) {
		if (addObjects == null) {
			addObjects = new HashMap<Integer, Object>();
		}
		addObjects.put(hash, o);
	}
	
	void removeObject(int hash) {
		if (removeObjects == null) {
			removeObjects = new ArrayList<Integer>();
		}
		removeObjects.add(hash);
	}
	
	void removeObject(Collection<Integer> hashes) {
		if (removeObjects == null) {
			removeObjects = new ArrayList<Integer>();
		}
		removeObjects.addAll(hashes);
	}

	public Map<String, ComponentClass> getAddClasses() {
		return addClasses;
	}

	public List<String> getRemoveClasses() {
		return removeClasses;
	}

	public Map<Integer, Object> getAddObjects() {
		return addObjects;
	}

	public List<Integer> getRemoveObjects() {
		return removeObjects;
	}
	
}
